<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增课程')" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-course-add">
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">课程编码：</label>
                <div class="col-sm-8">
                    <input name="courseCode" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">课程名称：</label>
                <div class="col-sm-8">
                    <input name="coName" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">课程视频：</label>
                <div class="col-sm-8">

                    <!-- input用来存储上传路径到数据库 -->
                    <input id="technician_video" name="courseVideoUrl " class="form-control" type="text" style="display: none">
                    <div class="fileinput fileinput-new input-group" data-provides="fileinput" id="inputTechnicianVideo">
                        <!-- 此div用来回显预览上传完成后的视频 -->
                        <div class="fileinput-preview fileinput-exists thumbnail" id="viderSource">
                        </div>
                        <!--相关按钮等 -->
                        <div>
                        <span class="btn btn-white btn-file">
                            <span class="fileinput-new">选择文件</span>
                            <span class="fileinput-exists">更改</span>
                            <input type="file" id="technicianVideo">
                        </span>
                            <a href="#" class="btn btn-white fileinput-exists" data-dismiss="fileinput">清除</a>
                        </div>
                    </div>


                    <!--<input name="courseVideoUrl" id="pic" class="form-control" type="hidden">
                    <a id="url"><img th:src="*{courseVideoUrl}" style="width: 200px;height: 200px"></a>
                    <input type="file" id="abc">-->

                   <!-- <input type="hidden" name="courseVideoUrl">
                    <div class="file-loading">
                        <input class="form-control file-upload" id="courseVideoUrl" name="file" type="file">
                    </div>-->
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">课程售价：</label>
                <div class="col-sm-8">
                    <input name="money" class="form-control" type="text">
                </div>
            </div>
            <!--<div class="form-group">
                <label class="col-sm-3 control-label">观看人数：</label>
                <div class="col-sm-8">
                    <input name="people" class="form-control" type="text">
                </div>
            </div>-->
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">教师姓名：</label>
                <div class="col-sm-8">
                    <select class="form-control m-b" id="teacherCode" name="teacherCode">
                        <option th:each="a : ${all1}" th:text="${a.teacherName}" th:value="${a.teacherCode}"></option>
                    </select>
                    <!--<input name="teacherCode" class="form-control" type="text" required>-->
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">课程类别：</label>
                <div class="col-sm-8">
                    <select class="form-control m-b" id="caCode" name="caCode">
                        <option th:each="v : ${all}" th:text="${v.caName}" th:value="${v.caCode}"></option>
                    </select>
                    <!--<select name="caCode" class="form-control m-b" required>
                        <option value="">所有</option>
                    </select>
                    <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 代码生成请选择字典属性</span>-->
                </div>
            </div>
            <!--<div class="form-group">
                <label class="col-sm-3 control-label">课程评论编码：</label>
                <div class="col-sm-8">
                    <input name="coCode" class="form-control" type="text">
                </div>
            </div>-->
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-fileinput-js"/>
    <script th:inline="javascript">
        var prefix = ctx + "system/course"
        $("#form-course-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-course-add').serialize());
            }
        }

        $(".file-upload").fileinput({
            uploadUrl: ctx + 'common/upload',
            maxFileCount: 1,
            autoReplace: true
        }).on('fileuploaded', function (event, data, previewId, index) {
            $("input[name='" + event.currentTarget.id + "']").val(data.response.url)
        }).on('fileremoved', function (event, id, index) {
            $("input[name='" + event.currentTarget.id + "']").val('')
        })

        /*上传视频开始*/
        $('#inputTechnicianVideo').on('change.bs.fileinput ', function (e) {
            let imgFile = $('#technicianVideo')[0].files[0];
            let video = "video";
            uploadFile(imgFile, video);
        });
        //ruoyi通用的上传接口，不需要自己写后台，框架自带
        function uploadFile(imgFile, imgId) {
            var formData = new FormData();
            if (imgFile == null) {
                $.modal.alertWarning("请先选择文件路径");
                return false;
            }
            formData.append('file', imgFile);
            $.ajax({
                url: "/common/upload",
                type: 'post',
                cache: false,
                data: formData,
                processData: false,
                contentType: false,
                dataType: "json",
                async: false,
                success: function (result) {
                    //给input赋值
                    $('#technician_video').val(result.fileName);
                    //用来回显预览视频
                    let videoElement = '<video controls="controls" width="100%"><source type="video/mp4" src="' + result.url + '"/></video>\n';
                    $("#viderSource").append(videoElement);
                }
            });
        }
        /*上传结束*/


    </script>
</body>
</html>